<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>单词对对碰</title>
    <style>
        /* 全局样式 */
        body {
            font-family: 'Arial', sans-serif;
            background: linear-gradient(135deg, #f4f4f9, #e0e0e7);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            margin: 0;
            padding: 20px;
            color: #333;
        }

        h1 {
            font-size: 2rem;
            color: #6200ea;
            margin-bottom: 20px;
            text-align: center;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
        }

        /* 分类选择框 */
        #category-select {
            padding: 10px 15px;
            font-size: 1rem;
            border: 2px solid #6200ea;
            border-radius: 8px;
            background-color: white;
            color: #333;
            margin-bottom: 20px;
            cursor: pointer;
            transition: border-color 0.3s, box-shadow 0.3s;
            width: 100%;
            max-width: 300px;
        }

        #category-select:hover {
            border-color: #3700b3;
            box-shadow: 0 0 8px rgba(98, 0, 234, 0.3);
        }

        /* 得分显示 */
        #score {
            font-size: 1.5rem;
            font-weight: bold;
            color: #03dac6;
            margin-bottom: 20px;
            text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        /* 游戏棋盘 */
        #game-board {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
            gap: 10px;
            width: 100%;
            max-width: 400px;
            margin-bottom: 20px;
        }

        /* 卡片样式 */
        .card {
            width: 100%;
            aspect-ratio: 1 / 1;
            background-color: #6200ea;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.2rem;
            cursor: pointer;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s, background-color 0.3s, opacity 0.3s, box-shadow 0.3s;
            position: relative;
            transform-style: preserve-3d;
        }

        .card.flipped {
            background-color: #03dac6;
            transform: rotateY(180deg);
        }

        .card.matched {
            opacity: 0;
            pointer-events: none;
            transform: scale(0.9);
        }

        .card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(255, 255, 255, 0.1);
            border-radius: 10px;
            opacity: 0;
            transition: opacity 0.3s;
        }

        .card:hover::before {
            opacity: 1;
        }

        /* 按钮样式 */
        #restart-button, #manage-words-link {
            padding: 10px 20px;
            font-size: 1rem;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            transition: background-color 0.3s, transform 0.3s;
            margin: 5px;
            width: 100%;
            max-width: 200px;
            text-align: center;
        }

        #restart-button {
            background-color: #6200ea;
            color: white;
        }

        #restart-button:hover {
            background-color: #3700b3;
            transform: translateY(-2px);
        }

        #manage-words-link {
            background-color: #03dac6;
            color: white;
            text-decoration: none;
        }

        #manage-words-link:hover {
            background-color: #018786;
            transform: translateY(-2px);
        }

        /* 游戏结束提示 */
        #game-over-modal {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: white;
            padding: 20px;
            border-radius: 15px;
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
            text-align: center;
            z-index: 1000;
            width: 90%;
            max-width: 300px;
        }

        #game-over-modal h2 {
            font-size: 1.5rem;
            color: #6200ea;
            margin-bottom: 20px;
        }

        #game-over-modal p {
            font-size: 1rem;
            color: #333;
            margin-bottom: 20px;
        }

        #game-over-modal button {
            padding: 10px 20px;
            font-size: 1rem;
            background-color: #6200ea;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            transition: background-color 0.3s, transform 0.3s;
            width: 100%;
            max-width: 200px;
        }

        #game-over-modal button:hover {
            background-color: #3700b3;
            transform: translateY(-2px);
        }

        /* 手机端适配 */
        @media (max-width: 600px) {
            h1 {
                font-size: 1.5rem;
            }

            #score {
                font-size: 1.2rem;
            }

            .card {
                font-size: 1rem;
            }

            #game-board {
                grid-template-columns: repeat(auto-fit, minmax(70px, 1fr));
                gap: 8px;
            }

            #restart-button, #manage-words-link {
                font-size: 0.9rem;
                padding: 8px 16px;
            }

            #game-over-modal h2 {
                font-size: 1.2rem;
            }

            #game-over-modal p {
                font-size: 0.9rem;
            }
        }
    </style>
</head>
<body>
    <h1 id="category-title">单词对对碰</h1>
    <select id="category-select">
        <option value="">请选择分类</option>
    </select>
    <div id="score">得分: 0</div>
    <div id="game-board"></div>
    <div>
        <button id="restart-button">重新开始</button>
        <a href="manage.html" id="manage-words-link">管理单词</a>
    </div>

    <!-- 游戏结束提示 -->
    <div id="game-over-modal">
        <h2>游戏结束！</h2>
        <p id="final-score">最终得分: 0</p>
        <button id="play-again-button">再玩一次</button>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const gameBoard = document.getElementById('game-board');
            const categorySelect = document.getElementById('category-select');
            const restartButton = document.getElementById('restart-button');
            const scoreDisplay = document.getElementById('score');
            const gameOverModal = document.getElementById('game-over-modal');
            const finalScoreDisplay = document.getElementById('final-score');
            const playAgainButton = document.getElementById('play-again-button');

            let selectedCards = [];
            let matchedPairs = 0;
            let score = 0;
            let words = []; // 存储当前分类的单词列表

            // 加载分类选项
            function loadCategories() {
                const categories = JSON.parse(localStorage.getItem('categories')) || {};
                categorySelect.innerHTML = '<option value="">请选择分类</option>';
                Object.keys(categories).forEach(category => {
                    const option = document.createElement('option');
                    option.value = category;
                    option.textContent = category;
                    categorySelect.appendChild(option);
                });
            }

            // 创建单词卡片
            function createCard(word, isChinese) {
                const card = document.createElement('div');
                card.classList.add('card');
                card.dataset.word = isChinese ? word.chinese : word.english;
                card.dataset.language = isChinese ? 'chinese' : 'english';
                card.dataset.pairId = word.chinese; // 使用中文作为配对标识
                card.textContent = isChinese ? word.chinese : word.english;
                card.addEventListener('click', flipCard);
                return card;
            }

            // 翻转卡片
            function flipCard(event) {
                const card = event.target;
                if (selectedCards.length < 2 && !card.classList.contains('flipped')) {
                    card.classList.add('flipped');
                    selectedCards.push(card);

                    if (selectedCards.length === 2) {
                        checkMatch();
                    }
                }
            }

            // 检查是否匹配
            function checkMatch() {
                const [card1, card2] = selectedCards;
                const language1 = card1.dataset.language;
                const language2 = card2.dataset.language;
                const pairId1 = card1.dataset.pairId;
                const pairId2 = card2.dataset.pairId;

                // 检查是否一张是中文，一张是英文，且配对标识相同
                if (
                    (language1 === 'chinese' && language2 === 'english' && pairId1 === pairId2) ||
                    (language1 === 'english' && language2 === 'chinese' && pairId1 === pairId2)
                ) {
                    // 匹配成功，卡片直接消失
                    setTimeout(() => {
                        card1.classList.add('matched');
                        card2.classList.add('matched');
                        playSound('win.mp3');
                        matchedPairs++;
                        score += 10; // 每次匹配成功加10分
                        scoreDisplay.textContent = `得分: ${score}`;

                        // 检查是否全部匹配成功
                        if (matchedPairs === words.length) {
                            showGameOverModal();
                        }
                    }, 500); // 延迟500ms让用户看到匹配效果
                } else {
                    // 匹配失败，翻转回背面
                    setTimeout(() => {
                        card1.classList.remove('flipped');
                        card2.classList.remove('flipped');
                        playSound('lose.mp3');
                    }, 1000);
                }
                selectedCards = [];
            }

            // 显示游戏结束提示
            function showGameOverModal() {
                finalScoreDisplay.textContent = `最终得分: ${score}`;
                gameOverModal.style.display = 'block';
            }

            function playSound(soundFile) {
                const audio = new Audio(soundFile);
                audio.play();
            }

            // 隐藏游戏结束提示
            function hideGameOverModal() {
                gameOverModal.style.display = 'none';
            }

            // 随机打乱数组
            function shuffle(array) {
                for (let i = array.length - 1; i > 0; i--) {
                    const j = Math.floor(Math.random() * (i + 1));
                    [array[i], array[j]] = [array[j], array[i]];
                }
            }

            // 开始游戏
            function startGame(category) {
                const categories = JSON.parse(localStorage.getItem('categories')) || {};
                words = categories[category] || []; // 更新当前分类的单词列表
                const allWords = [];
                words.forEach(word => {
                    allWords.push({ ...word, isChinese: true }); // 中文卡片
                    allWords.push({ ...word, isChinese: false }); // 英文卡片
                });
                shuffle(allWords);

                gameBoard.innerHTML = '';
                allWords.forEach(word => {
                    const card = createCard(word, word.isChinese);
                    gameBoard.appendChild(card);
                });

                document.getElementById('category-title').textContent = `分类: ${category}`;
                matchedPairs = 0;
                score = 0;
                scoreDisplay.textContent = `得分: ${score}`;
                hideGameOverModal();
            }

            // 初始化
            loadCategories();
            categorySelect.addEventListener('change', (event) => {
                const category = event.target.value;
                if (category) {
                    startGame(category);
                }
            });

            restartButton.addEventListener('click', () => {
                const category = categorySelect.value;
                if (category) {
                    startGame(category);
                }
            });

            playAgainButton.addEventListener('click', () => {
                const category = categorySelect.value;
                if (category) {
                    startGame(category);
                }
            });
        });
    </script>
</body>
</html>